package com.sample.navigation.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sample.navigation.R;

import java.util.ArrayList;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private ArrayList<TypedViewData> dataList;
    private Context context;
    private OnItemClickListener onItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;

    public interface OnItemClickListener{
        void onClick(View view, int position);
        void onSelect(View view, int position);
    }

    public interface OnItemLongClickListener{
        void onLongClick(View view, int position);
        void onRemove(View view, int position);
    }

    public RecyclerViewAdapter(ArrayList<TypedViewData> dataList, Context context){
        this.dataList = dataList;
        this.context = context;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener){
        this.onItemLongClickListener = onItemLongClickListener;
    }

    private final class SpinnerTextViewHolder extends RecyclerView.ViewHolder{
        private TextView textSpinner;
        public SpinnerTextViewHolder(View view){
            super(view);
            textSpinner = (TextView)view.findViewById(R.id.rv_item_spinner_text);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return dataList.get(position).getType();
    }
    
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if(i == TypedViewData.TYPE_SPINNER_TEXT){
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(
                    R.layout.rv_item_spinner, viewGroup, false);
            SpinnerTextViewHolder VH = new SpinnerTextViewHolder(view);
            return VH;
        }
        return null;

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, @SuppressLint("RecyclerView") int i) {
        final int position = i;
        if(viewHolder instanceof SpinnerTextViewHolder){
            ((SpinnerTextViewHolder) viewHolder).textSpinner.setText(dataList.get(i).getStringValue()[0]);
            if(dataList.get(i).getBoolValue()[0]){
                viewHolder.itemView.setBackgroundColor(context.getResources().getColor(R.color.bg_highlight));
                ((SpinnerTextViewHolder) viewHolder).textSpinner.setTextColor(
                        context.getResources().getColor(R.color.text_color_highlight));
            }else{
                viewHolder.itemView.setBackgroundColor(context.getResources().getColor(R.color.transparent));
                ((SpinnerTextViewHolder) viewHolder).textSpinner.setTextColor(
                        context.getResources().getColor(R.color.text_color_spinner));
            }
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener != null){
                        onItemClickListener.onSelect(v, position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }
}
